package com.mz.ozy.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.multilevel.treelist.Node;
import com.multilevel.treelist.TreeRecyclerAdapter;
import com.mz.ozy.R;

import java.util.List;

/**
 * Created by zhangke on 2017-1-14.
 */
public class CommunityLevelAdapter extends TreeRecyclerAdapter {

    public CommunityLevelAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
    }

    public CommunityLevelAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHoder(View.inflate(mContext, R.layout.list_item, null));
    }

    @Override
    public void onBindViewHolder(final Node node, RecyclerView.ViewHolder holder, int position) {

        final MyHoder viewHolder = (MyHoder) holder;
        viewHolder.itemView.setPadding(node.getLevel() * 70, 3, 3, 3);
        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }
        String[] s = node.getName().split("_");
        viewHolder.tv_username.setText(s[4]);
        viewHolder.tv_yj.setText(s[5] + "/" + s[1]);
        final String personlabel = mContext.getString(R.string.person_str);
        viewHolder.tv_assets_packet.setText(s[2] + personlabel + "/" + s[8] + personlabel);

        if ("0".equals(s[6])) {
            viewHolder.item_root_view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        } else if ("1".equals(s[6])) {
            viewHolder.item_root_view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_e3e3e6));
        } else {
            viewHolder.item_root_view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_ebebed));
        }

        viewHolder.tv_name.setText("(" + s[7] + ")");
        switch (Integer.parseInt(s[0])) {
            case 1:
                viewHolder.tv_userType.setText(R.string.personal_node);
                break;
            case 2:
                viewHolder.tv_userType.setText(R.string.super_node);
                break;
            case 0:
            default:
                viewHolder.tv_userType.setText(R.string.common_node);
                break;
        }

    }

    class MyHoder extends RecyclerView.ViewHolder {

        RelativeLayout item_root_view;
        public TextView tv_username;
        public TextView tv_name;
        public TextView tv_userType;
        public TextView tv_yj;
        public TextView tv_assets_packet;

        public ImageView icon;

        public MyHoder(View itemView) {
            super(itemView);
            item_root_view = (RelativeLayout) itemView
                    .findViewById(R.id.item_root_view);
            tv_username = (TextView) itemView
                    .findViewById(R.id.tv_username);
            tv_name = (TextView) itemView
                    .findViewById(R.id.tv_name);
            tv_userType = (TextView) itemView
                    .findViewById(R.id.tv_userType);
            tv_yj = (TextView) itemView
                    .findViewById(R.id.tv_yj);
            tv_assets_packet = (TextView) itemView
                    .findViewById(R.id.tv_assets_packet);

            icon = (ImageView) itemView.findViewById(R.id.icon);

        }

    }
}
